package cn.johnyu.persistencdemo01;

import android.content.SharedPreferences;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Environment;
import android.preference.PreferenceManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

import java.io.File;
import java.io.InputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.util.HashSet;
import java.util.Set;

public class MainActivity extends AppCompatActivity {
    private SharedPreferences pref;
    private SQLiteDatabase db;
    private JohnOpenHelper helper;
    private int i=0;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        pref= PreferenceManager.getDefaultSharedPreferences(this);
        File sdcardPath= Environment.getExternalStorageDirectory();
        Log.i("john",sdcardPath+"........");
        helper=new JohnOpenHelper(this,sdcardPath.getAbsolutePath()+"/john31.db",null,2);
        db=helper.getWritableDatabase();


    }

    public void storeToFile(View view) throws Exception{
        OutputStream out=openFileOutput("john.data",MODE_PRIVATE|MODE_APPEND);
       // ObjectOutputStream oout=new ObjectOutputStream(out);
        //oout.writeObject(new Object());

        out.write("hello Wrold,你好世界!".getBytes("UTF-8"));
        out.close();
    }
    public void getFromFile(View view) throws Exception{
        InputStream in=openFileInput("john.data");
//        byte[] buf=new byte[4];
//        int count=0;
//        while ((count=in.read(buf))!=-1){
//
//        }
        byte[] buf=new byte[in.available()];
        in.read(buf);
        String msg=new String(buf,"UTF-8");
        Log.i("john", msg);
        in.close();



    }

    public void storeToPref(View view) {
        Set<String> set=new HashSet<String>();
        set.add("basket");set.add("foot");
        SharedPreferences.Editor editor=pref.edit();
        editor.putString("uname", "john");
        editor.putInt("age", 100);
        editor.putStringSet("fav", set);
        editor.commit();
    }
    public void getFromPref(View view) {
        String uname=pref.getString("uname","xxxxx");
        int age=pref.getInt("age", -1);
        Set<String> fav=pref.getStringSet("fav",null);
        Log.i("john", uname + "...." + age);
        for(String key:fav){
            Log.i("john",key);
        }
    }

    public void storeToDB(View view) {
        String sql="insert into users (uname) values (?)";
        try {
            db.beginTransaction();
            db.execSQL(sql,new Object[]{"john.."+i++});
            db.setTransactionSuccessful();
        }
        catch (Exception e){

        }
        finally {
            db.endTransaction();
        }

    }

    public void getFromDB(View view) {
        String sql="select _id,uname from users where _id>?";
        Cursor c= db.rawQuery(sql, new String[]{"2"});
        while (c.moveToNext()){
            String uname=c.getString(1);
            int id=c.getInt(c.getColumnIndex("_id"));
            Log.i("john",uname+"....."+id);
        }

    }


}
